<template>
  <el-dialog title="员工导入" :visible="importVisible" width="500px" @close="$emit('update:importVisible', false)">
    <el-row type="flex" justify="center">
      <div class="upload-excel">
        <input
          ref="excel-upload-input"
          class="excel-upload-input"
          type="file"
          accept=".xlsx, .xls" 
          @change="upload"/>    <!-- 选择文件后会触发change事件 -->
        <div class="drop">
          <i class="el-icon-upload" />
          <el-button type="text" @click="importDemo">下载导入模板</el-button>
          <span>将文件拖到此处或
            <el-button type="text" @click="uploadFile">点击上传</el-button>
          </span>
        </div>
      </div>
    </el-row>
    <el-row type="flex" justify="end">
      <el-button size="small" type="primary" @click="$emit('update:importVisible', false)">取 消</el-button>
    </el-row>
  </el-dialog>
</template>

<script>
import { importDemo, uploadExcel } from '@/api/employee'
import FileSaver from 'file-saver'

export default {
  name: 'ImportExcel',
  props: {
    importVisible: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    // 下载导入模板
    async importDemo() {
      // 调用下载导入模板的接口
      const result = await importDemo()
      FileSaver.saveAs(result, '员工导入模板.xlsx')
    },
    // 上传Excel文件的方法
    uploadFile() {
      this.$refs['excel-upload-input'].click()
    },
    // 选择文件后调用的方法
    async upload(e) {
      // 取到选择的Excel文件
      const files = e.target.files
      // 调用上传接口需要判断是否取到了文件
        if (files.length > 0) {
          // 把文件整理成可供 HTTP 请求传输的数据体 multipart/form-data
          const data = new FormData()
          data.append('file', files[0])
          try{
            await uploadExcel(data)
            // 上传成功 则重新加载数据
            this.$emit('uploadSuccess')
            // 弹出成功消息
            this.$message.success('表格导入成功')
            // 关闭弹层
            this.$emit('update:importVisible', false)
            // 清空文件选择器
            // this.$refs['excel-upload-input'].value = ''
          }catch(error){
            // 上传失败  只清空文件选择器
            // this.$refs['excel-upload-input'].value = ''
          } finally {
            // 不论成功还是失败最后都会执行这个块，写在这里可以简化代码
            this.$refs['excel-upload-input'].value = ''
          }
        }
    }
  }
}
</script>

<style scoped lang="scss">
    .upload-excel {
      display: flex;
      justify-content: center;
      margin: 20px;
      width: 360px;
      height: 180px;
      align-items: center;
      color: #697086;
      .excel-upload-input {
        display: none;
        z-index: -9999;
      }
      .btn-upload,
      .drop {
        border: 1px dashed #dcdfe6;
        width: 100%;
        height: 100%;
        text-align: center;
        line-height: 160px;
        border-radius: 8px;
        display: flex;
        flex-direction: column;
        justify-content: center;
      }
      .drop {
        line-height: 40px;
        color: #bbb;
        i {
          font-size: 60px;
          display: block;
          color: #c0c4cc;
        }
      }
    }
</style>